<template>
  <div class="new-table-style">
    <section class="search-style-box" v-if="activeTab == 6">
      <el-form ref="searchFrom" label-width="100px" size="small" :model="searchParams">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="开始时间">
              <el-date-picker v-model="searchParams.startDate" type="date" value-format="timestamp" :picker-options="pickerOptionsStart" clearable placeholder="请选择"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结束时间">
              <el-date-picker v-model="searchParams.endDate" type="date" value-format="timestamp" :picker-options="pickerOptions" clearable placeholder="请选择"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资产编码">
              <el-select v-model="searchParams.chargeItemCode" clearable filterable>
                <el-option v-for="item in options.assetList" :key="item.shopCode" :label="item.shopNumber" :value="item.shopCode" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="品牌名称">
              <el-select v-model="searchParams.operateBrand" clearable filterable>
                <el-option v-for="item in options.brandList" :key="item.primaryKey" :label="item.catalogName" :value="item.primaryKey" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="客户名称">
              <el-select v-model="searchParams.customerKeys" clearable filterable>
                <el-option v-for="item in options.clientList" :key="item.primaryKey" :label="item.partnerName" :value="item.primaryKey" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="应收类型">
              <el-select v-model="searchParams.receivableType" clearable filterable>
                <el-option v-for="item in options.gatherTypeList" :key="item.key" :label="item.value" :value="item.key" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="费项">
              <el-select v-model="searchParams.assetType" clearable filterable>
                <el-option v-for="item in options.chargeList" :key="item.key" :label="item.value" :value="item.key" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="应收状态">
              <el-select v-model="searchParams.receivableStatus" clearable filterable>
                <el-option v-for="item in billStatusList" :key="item.key" :label="item.value" :value="item.key" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-button type="primary" size="mini" class="f-right" @click="getList('')">查询</el-button>
            <el-button type="primary" size="mini" class="f-right margin-right-10" @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </section>
    <el-table :border="true" :data="tableData" align="left" v-if="tableData.length || activeTab != 6">
      <el-table-column v-if="activeTab == '6'" label="应收类型">
        <template slot-scope="scope">
          <span>{{ getReceivvaleName(scope.row.receivableType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应收状态">
        <template slot-scope="scope">
          <span v-if="scope.row.paymentStatus == 1 || scope.row.paymentStatus == 2">
            <span>
              距离缴费<br />
              {{ Math.abs(scope.row.countDays) }}天
            </span>
          </span>
          <span v-else-if="scope.row.paymentStatus == 3" class="color-red">
            逾期<br />
            {{ Math.abs(scope.row.countDays) }}天
          </span>
          <span v-else-if="scope.row.paymentStatus == 4">已结清</span>
          <span v-else-if="scope.row.paymentStatus == 6">已退款</span>
          <span v-else-if="scope.row.paymentStatus == 7">已冲账</span>
        </template>
      </el-table-column>
      <el-table-column label="账单说明" prop="billDesc">
        <template slot-scope="scope">
          <div class="font-size-16">{{ scope.row.customerNames }}</div>
          <div v-if="scope.row.chargeItemName">
            <span>({{ scope.row.chargeItemName }}</span>
            <span v-if="scope.row.assetType">-{{ getSpaceName(scope.row.assetType) }})</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="费项" prop="bizTypeName"></el-table-column>
      <el-table-column label="应缴费日期">
        <template slot-scope="scope">
          <span>{{ scope.row.paymentDeadline | dateFormat('YYYY-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应收金额(元)">
        <template slot-scope="scope">
          <span>{{ scope.row.amountReceivable | fmoneyThousand }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实收金额(元)">
        <template slot-scope="scope">
          <span>{{ scope.row.amountReceived | fmoneyThousand }}</span>
        </template>
      </el-table-column>
      <el-table-column label="未缴纳金额(元)">
        <template slot-scope="scope">
          <span>{{ (scope.row.amountReceivable - scope.row.amountReceived) | fmoneyThousand }}</span>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-if="total > 0" @change="pageChange" :total="total" paginationLayout="total, prev, pager, next" />

    <div class="absolute-box btn-box">
      <el-button type="primary" size="mini" @click="exportFn">导出</el-button>
    </div>
  </div>
</template>

<script>
  import { apiGatherList, apiGatherExport } from '../../../api/gatherManage';

  export default {
    name: 'billTable',
    props: {
      activeTab: {
        type: String,
        default: '',
      },
      options: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
    data() {
      return {
        pickerOptions: {
          disabledDate: time => {
            if (this.searchParams.startTime) {
              return time.getTime() < this.searchParams.startTime;
            }
          },
        },
        pickerOptionsStart: {
          disabledDate: time => {
            if (this.searchParams.endTime) {
              return time.getTime() > this.searchParams.endTime;
            }
          },
        },
        assetTypeList: [],
        customList: [],
        billTypeList: [],
        billStatusList: [
          { key: 2, value: '未付款' },
          { key: 3, value: '逾期' },
          { key: 4, value: '已结清' },
        ],
        searchParams: {},
        tableData: [],
        total: 0,
        currentPage: 1,
        pageSize: 10,
        statusList: ['', '未结清', '未缴费', '逾期', '已付款'],
        typeList: ['', 'rent', 'manage', 'other', 'deposit', 'agencyFund', null],
      };
    },
    methods: {
      init() {
        if (this.activeTab != 6) {
          this.getList();
        } else {
          this.tableData = []
          this.currentPage = 1
          this.total = 0
        }
      },

      reset() {
        this.searchParams = {}
        this.getList('');
      },

      //数据源
      async getList(more) {
        let res = await apiGatherList({
          currentPage: more ? this.currentPage : 1,
          pageSize: this.pageSize,
          receivableType: this.typeList[this.activeTab],
          ...this.searchParams,
        });
        this.tableData = res.rows || [];
        this.total = res.total || 0;
      },

      //分页
      pageChange(page) {
        this.currentPage = page.currentPage;
        this.getList('more');
      },

      getSpaceName(key){
        let target = this.options.spaceTypeList.find(item => item.key == key)
        return target ? target.value : ""
      },
      
      getReceivvaleName(key){
        let target = this.options.gatherTypeList.find(item => item.key == key)
        return target ? target.value : ""
      },

      getChargeTxt(key){
        let target = this.options.chargeList.find(item => item.key == key)
        return target ? target.value : ""
      },

      //导出
      exportFn() {
        apiGatherExport({
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          receivableType: this.typeList[this.activeTab],
          ...this.searchParams,
        });
      },
    },
  };
</script>

<style lang="scss" scoped>
  .search-style-box {
    background: #fbfbfb;
    border-radius: 3px;
    padding: 20px 20px 0;
    margin-bottom: 20px;
  }

  .btn-box {
    right: 0;
    top: -45px;
  }
</style>
